package cn.easybuy.web.pre;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.easybuy.entity.News;
import cn.easybuy.entity.Product;
import cn.easybuy.entity.Product_category;
import cn.easybuy.service.news.NewsService;
import cn.easybuy.service.news.NewsServiceImpl;
import cn.easybuy.service.product.ProductService;
import cn.easybuy.service.product.ProductServiceImpl;
import cn.easybuy.service.product.Product_CategoryService;
import cn.easybuy.service.product.Product_CategoryServiceImpl;

public class IndexServlet extends HttpServlet {

	Product_CategoryService pcs=new Product_CategoryServiceImpl();
	NewsService ns = new NewsServiceImpl();
	ProductService ps = new ProductServiceImpl();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();

		String action = request.getParameter("action");
		if(action.equals("f5")){
			session.removeAttribute("productList");
			response.sendRedirect("IndexServlet?action=show");
		}else if(action.equals("show")){

			//获取一级--三级菜单集合
			List<List<Product_category>> list=pcs.getList();
			//获取最新新闻资讯
			List<News> newsList=ns.findAllNews();
			//获取所有商品
			List<Product> productList=ps.findProduct();
			//存储
			session.setAttribute("categoryList", list);
			session.setAttribute("newsList", newsList);
			session.setAttribute("productList", productList);
			response.sendRedirect("Index.jsp");
		}
	}

}
